package ua.races.jsptag;

import ua.races.dao.RacesDaoImpl;
import org.apache.log4j.Logger;
import ua.races.vo.*;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class ViewRaceForBetTag extends TagSupport {

    private static final Logger log = Logger.getLogger(PreviousBetsTag.class.getName());


    private long raceId;

    public void setRaceId(String raceId) {
        this.raceId = Long.parseLong(raceId);
    }

    @Override
    public int doStartTag() throws JspException {

//        List<Bet> bets = null;
        RacesDaoImpl dao;
        Race race = null;
        List<Horse> horses = null;
        List<BetType> betTypes = null;


        dao = (RacesDaoImpl) pageContext.getServletContext().getAttribute("DAO");

        try {
            race = dao.findRaceById(raceId);
            betTypes = dao.findAllBetTypes();
            //todo dao.findAllHorsesInRace(race);
            horses = dao.findAllHorses();
        } catch (SQLException e) {
            log.error(e);
        }

        JspWriter w = pageContext.getOut();
        try {
            w.println("<form action=\"addBetToDb.do\"  method=\"Get\">");
            w.println("<table summary=\"\" border=\"1\" bgcolor=\"#add8e6\">");
            w.println("<thead bgcolor = \"#e9967a\" >");
            w.println("<tr >\n" +
                    "        <th > Date </th >\n" +
                    "        <th > Time </th >\n" +
                    "        <th > Stadium </th >\n" +
                    "</tr >\n" +
                    "</thead >\n" +
                    "<tbody>");

            w.println("<tr >\n" +
                    "        <td ><input type = \"hidden\" name = \"raceId\" value = \"" + race.getRaceId() + "\"\">" 
                    + race.getRaceDate() + "</td>");
            w.println("<td>" + race.getStartTime() + "</td>");
            w.println("<td>" + race.getStadium() + "</td >\n</tr>");
            w.println("</tbody >\n  </table>");


            //todo sdfksda;lfg;adlskfg

            w.println("<h1>Choose bet type</h1><select title=\"Choose bet type\"  name=\"betType\" >\n");
            for (BetType bt : betTypes) {
                w.println("<option>" + bt.getBetType() + "</option>\n");
            }
            w.println("</select>");

            w.println("<table summary=\"\" border=\"1\" bgcolor=\"#add8e6\">");
            w.println("<thead bgcolor = \"#e9967a\" >");
            w.println("<tr >\n" +
                    "        <th > Horse </th >\n" +
                    "        <th > History </th >\n" +
                    "        <th > Value </th >\n" +
                    "        <th > Bet </th >\n" +
                    "</tr >\n" +
                    "</thead >\n" +
                    "<tbody>");
            for (Horse h : horses) {
                w.println("<tr >\n" +
                        "        <td ><input type = \"hidden\" name = \"horseId\" value = \"" + h.getHorseId() + "\">"
                        + h.getHorseName() + "</td>");
                //w.println("<td>" + r.getRaceDate() + "</td>");
                w.println("<td>history under construction</td>");
                w.println("<td><input type=\"text\" name=\"betValue\" ></td>");
                w.println("<td ><input type=\"checkbox\" name=\"CHECK\"/>");
            }

            w.println("</tbody >\n" +
                    "        </table > " +
                    "<input type=\"submit\" value=\"add bet\"> " +
                    "<input type=\"hidden\" name=\"clientId\" value=\"" +
                    ((Client)pageContext.getSession().getAttribute("client")).getClientId() +
                    "\"/></form>");


            //todo dfsdsdfasdfasdfasdfas


        } catch (IOException e) {
            log.error(e);
        }

        return SKIP_BODY;

    }

    @Override
    public int doEndTag
            () throws JspException {
        return EVAL_PAGE;
    }
}
